﻿Imports DTO
Imports System.Windows.Forms

Public Class ChiTietPNSachDAO
    Public Sub Them(ByVal ctpnDTO As ChiTietPNDTO)
        Try
            Dim db As New QuanLyNhaSachDataContext
            Dim ctpn As New ChiTietPN

            ctpn.MaPN = ctpnDTO.MaPN
            ctpn.TenSach = ctpnDTO.TenSach
            ctpn.MaTL = ctpnDTO.MaTL
            ctpn.TenTG = ctpnDTO.TenTG
            ctpn.SoLuong = ctpnDTO.SoLuong
            ctpn.DonGiaNhap = ctpnDTO.DonGiaNhap

            db.ChiTietPNs.InsertOnSubmit(ctpn)
            db.SubmitChanges()
            ctpnDTO.MaCTPN = ctpn.MaCTPN
            ctpnDTO.TenTG = ctpn.TenTG
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Public Function TimKiem(ByVal ctpnDTO As ChiTietPNDTO) As IQueryable(Of ChiTietPN)
        Dim db As New QuanLyNhaSachDataContext
        Dim ctpns = (From ct In db.ChiTietPNs _
                    Where ct.MaPN = ctpnDTO.MaPN And ct.TenSach = ctpnDTO.TenSach _
                    And ct.MaTL = ctpnDTO.MaTL And ct.TenTG = ctpnDTO.TenTG _
                    Select ct)
        Try
            ctpnDTO.MaCTPN = ctpns.Single.MaCTPN
        Catch ex As Exception
        End Try
        Return ctpns
    End Function

    Public Sub CapNhat(ByVal ctpnDTO As ChiTietPNDTO)
        Dim db As New QuanLyNhaSachDataContext
        Dim ctpn = db.ChiTietPNs.Single(Function(ct) ct.MaCTPN = ctpnDTO.MaCTPN)

        ctpn.TenSach = ctpnDTO.TenSach
        ctpn.MaTL = ctpnDTO.MaTL
        ctpn.TenTG = ctpnDTO.TenTG
        ctpn.SoLuong = ctpnDTO.SoLuong
        ctpn.DonGiaNhap = ctpnDTO.DonGiaNhap

        Try
            db.SubmitChanges()
        Catch ex As Exception
        End Try
    End Sub
End Class
